﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Epitech.Krisk.Xna.UI.Game.InGame.Gameplay
{
    public class Army
    {
        public static Army operator + (Army a1, Army a2)
        {
            Army tmp = new Army(a1.TotalSoldier() + a2.TotalSoldier());
            return tmp;
        }

        public override string ToString()
        {
            return TotalSoldier().ToString();
        }

        public static bool operator == (Army a1, int nb)
        {
            return a1.TotalSoldier() == nb;
        }

        public static bool operator >(Army a1, int nb)
        {
            return a1.TotalSoldier() > nb;
        }

        public static bool operator <(Army a1, int nb)
        {
            return a1.TotalSoldier() < nb;
        }

        public static bool operator >=(Army a1, int nb)
        {
            return a1.TotalSoldier() > nb;
        }

        public static bool operator <=(Army a1, int nb)
        {
            return a1.TotalSoldier() < nb;
        }

        public static bool operator !=(Army a1, int nb)
        {
            return a1.TotalSoldier() != nb;
        }

        public static Army operator - (Army a1, Army a2)
        {
            Army tmp = new Army();
            if (a2.TotalSoldier() <= a1.TotalSoldier())
                tmp.SetArmy(a1.TotalSoldier() - a2.TotalSoldier());
            else
                tmp.SetArmy(0);
            return tmp;
        }

        public static Army operator ++(Army a1)
        {
            a1.SetArmy(a1.TotalSoldier() + 1);
            return a1;
        }

        public static Army operator --(Army a1)
        {
            a1.SetArmy(a1.TotalSoldier() - 1);
            return a1;
        }

        private void SetArmy(int army)
        {
            WarPlane = army/10;
            army -= 10*WarPlane;
            Tank = army / 5;
            army -= 5 * Tank;
            Soldier = army;
        }

        public int TotalSoldier()
        {
            int total = Soldier;

            total += Tank*5;
            total += WarPlane*10;
            return total;
        }

        public int TotalTank()
        {
            int total = 0;

            total += Tank;
            total += WarPlane * 2;
            return total;
        }

        public int TotalWarPlane()
        {
            return WarPlane;
        }

        public Army()
        {
            WarPlane = 0;
            Tank = 0;
            Soldier = 0;
        }

        public Army(int army)
        {
            SetArmy(army);
        }

        public int WarPlane { get; set; }
        public int Tank { get; set; }
        public int Soldier { get; set; }

        public bool Equals(Army other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.WarPlane == WarPlane && other.Tank == Tank && other.Soldier == Soldier;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (Army)) return false;
            return Equals((Army) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = WarPlane;
                result = (result*397) ^ Tank;
                result = (result*397) ^ Soldier;
                return result;
            }
        }
    }
}
